const con=require("../db/db.js")
const showindex=(req,res)=>{
    let pagesize=3//每页条数;
    let nowpage=req.query.page ||1  //当前页 ;
    let offset=(nowpage-1)*pagesize;
    let sql=`SELECT a.title,a.id,a.ctime,u.nickname from blog_articles a
    LEFT JOIN blog_users u
    on a.authorId= u.id
    ORDER BY a.ctime DESC
    LIMIT ${offset},${pagesize};
    SELECT COUNT(*) as count from blog_articles`;
    con.query(sql,(err,result)=>{
      //console.log(result);
        if(err) return console.log("查询失败");
        let totalpage=Math.ceil(result[1][0].count/pagesize);//总条数
        // console.log(totalpage);
        res.render("index.ejs",{
            user:req.session.user,
            islogin:req.session.islogin,
            articles:result[0],
            nowpage:nowpage,
            totalpage:totalpage
        })
    })
}
const ctr={
    showindex
}
module.exports=ctr;